#include "windows.h"

int display_mode;
//DATA
int CP_X_calu,CP_Y_calu;
int ST,CP_X,CP_Y,TP_X,TP_Y,RN;
float SE;
char  SE_buf[30],CP_buf[30],TP_buf[30],RN_buf[30];

//PARA
int select_R_B;
float R = 1.0;
int B = 10;
char R_buf[30],B_buf[30];

//RECD
int B3_B4_ON_flag;
float TS;
int TT;
char TS_buf[30],TT_buf[30];

void display(void)
{
	if(display_mode == 0)
	{
		LCD_DisplayStringLine(Line1,(u8*)"        DATA       ");
		if(ST == 0)	LCD_DisplayStringLine(Line3,(u8*)"     ST=Idle      ");
		else if(ST == 1)	LCD_DisplayStringLine(Line3,(u8*)"     ST=Busy      ");
		else if(ST == 2)	LCD_DisplayStringLine(Line3,(u8*)"     ST=Wait      ");
		sprintf(CP_buf,"     CP=%d,%d      ",CP_X,CP_Y);
		LCD_DisplayStringLine(Line4,(u8*)CP_buf);
		sprintf(TP_buf,"     TP=%d,%d      ",TP_X,TP_Y);
		if(ST == 0) LCD_DisplayStringLine(Line5,(u8*)"     TP=NF      ");
		else LCD_DisplayStringLine(Line5,(u8*)TP_buf);
		sprintf(SE_buf,"     SE=%.1f      ",SE);
		if(ST == 0 || ST == 2)	LCD_DisplayStringLine(Line6,(u8*)"     SE=0.0      ");
		else LCD_DisplayStringLine(Line6,(u8*)SE_buf);
		sprintf(RN_buf,"     RN=%d      ",RN);
		if(ST == 0)	LCD_DisplayStringLine(Line7,(u8*)"     RN=NF      ");
		else LCD_DisplayStringLine(Line7,(u8*)RN_buf);
	}
	else if(display_mode == 1)
	{
		LCD_DisplayStringLine(Line1,(u8*)"        PARA       ");
		sprintf(R_buf,"     R=%.1f      ",R);
		LCD_DisplayStringLine(Line3,(u8*)R_buf);
		sprintf(B_buf,"     B=%d      ",B);
		LCD_DisplayStringLine(Line4,(u8*)B_buf);
		LCD_DisplayStringLine(Line5,(u8*)"                   ");
		LCD_DisplayStringLine(Line6,(u8*)"                   ");
		LCD_DisplayStringLine(Line7,(u8*)"                   ");
	}
	else if(display_mode == 2)
	{
		LCD_DisplayStringLine(Line1,(u8*)"        RECD       ");
		sprintf(TS_buf,"     TS=%.1f      ",TS);
		LCD_DisplayStringLine(Line3,(u8*)TS_buf);
		sprintf(TT_buf,"     TT=%d      ",TT);
		LCD_DisplayStringLine(Line4,(u8*)TT_buf);
		LCD_DisplayStringLine(Line5,(u8*)"                   ");
		LCD_DisplayStringLine(Line6,(u8*)"                   ");
		LCD_DisplayStringLine(Line7,(u8*)"                   ");
	}
}


void control_key(void)
{
	if(B1_ON)
	{
		while(B1_ON)
		{
			if(display_flag == 1)
			{
				display_flag = 0;
				display();
			}
			control_led(led);
		}
		if(ST == 0 && goal_x[0] != -1 && goal_y[0] != -1)
		{
			ST = 1;
		}
		else if(ST == 0 && goal_x[0] == -1 && goal_y[0] == -1)
		{
			ST = 0;
		}
		else if(ST == 1)
		{
			ST = 2;
		}
		else if(ST == 2)
		{
			ST = 1;
		}
	}
	else if(B2_ON)
	{
		while(B2_ON)
		{
			if(display_flag == 1)
			{
				display_flag = 0;
				display();
			}
			control_led(led);
		}
		if(display_mode == 0)
		{
			display_mode = 1;
			select_R_B = 0;
		}
		else if(display_mode == 1)
		{
			display_mode = 2;
		}
		else if(display_mode == 2)
		{
			display_mode = 0;
		}
	}
	else if(B3_ON)
	{
		uint32_t start = HAL_GetTick();
		while(B3_ON)
		{
			if(display_flag == 1)
			{
				display_flag = 0;
				display();
			}
			control_led(led);
			if(B4_ON)
			{
				B3_B4_ON_flag = 1;
			}
		}
		if(display_mode == 1)
		{
			select_R_B = 1 - select_R_B;
		}
		else if(display_mode == 0 && ST==0 && HAL_GetTick() - start >= 2000 && B3_B4_ON_flag == 1)
		{
			B3_B4_ON_flag = 0;
			TS = 0;
			TS_times = 0;
			TS_times_final = 0;
			TT = 0;
			TT_times = 0;
		}
	}
	else if(B4_ON)
	{
		while(B4_ON)
		{
			if(display_flag == 1)
			{
				display_flag = 0;
				display();
			}
			control_led(led);
		}
		if(display_mode == 1)
		{
			if(select_R_B == 0)
			{
				R += 0.1;
				if(R>2.0001)	R=1.0;
			}
			else if(select_R_B == 1)
			{
				B += 10;
				if(B>100)	B=10;
			}
		}
		
	}
}